/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path)
}

// 检验手机号
export const isValidatePhone = (phone) => {
	if (!/^1[3456789]\d{9}$/.test(phone)) {
		return false
	} else {
		return true
	}
}

// 密码验证
export const passwordCheck = (tips, value, callback) => {
	if (!value) {
		callback(new Error(`请输入${tips}密码！`))
	} else if (value.length < 6 || value.length > 15) {
		callback(new Error('请设置6到15位的密码！'))
	} else {
		callback()
	}
}

/**
 * 身份证格式校验
 */
export const isValidateIdentity = (phone) => {
	if (
		!/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(
			phone
		)
	) {
		return false
	} else {
		return true
	}
}

/**
 * 金额格式校验
 */
export const isAmount = (amount) => {
	if (
		!/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(
			amount
		)
	) {
		return false
	} else {
		return true
	}
}

/**
 * 百分比几率校验蔡徐坤
 * @param {number | string} Percent
 */
export const PercentagCheck = (Percent) => {
	const reg = /(^[0-9]{1,2}$)|(^[0-9]{1,2}[\.]{1}[0-9]{1,2}$)/
	return Percent.match(reg) === null
}

export function validatorPhone(rule, value, callback) {
	if (isValidatePhone(value)) {
		callback()
	} else {
		callback(new Error('请输入正确手机号码'))
	}
}

export const checkPrice = /^([0-9]{1}|^[1-9]{1}\d{1,15})(\.\d{1,2})?$/
